package com.zcgame.base.utils

import android.content.Context
import android.net.ConnectivityManager

/**
 *@author:huangLJ
 *@date:2021/5/27 19:48
 */
object NetUtils {
    // 是否连接了网络
    fun isConn(context: Context?): Boolean {
        var isNetWork = false
        if (context != null) {
            val connManager = context!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            if (connManager != null) {
                val network = connManager.activeNetworkInfo
                if (network != null) {
                    isNetWork = network.isAvailable
                }
            }

        }
        return isNetWork
    }

    /**
     * 判断wifi是否连接状态
     *
     * 需添加权限 android.permission.ACCESS_NETWORK_STATE
     *
     * @param context 上下文
     * @return true: 连接<br></br>false: 未连接
     */
    fun isWifiConnected(context: Context): Boolean {
        val cm = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        return cm?.activeNetworkInfo != null && cm.activeNetworkInfo!!.type == ConnectivityManager.TYPE_WIFI
    }
}